package date;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;

public class TestLocalDateTime03 {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        //以下方法的参数都是long型，返回值都是LocalDateTime
        //加
        LocalDateTime plusYearsResult = localDateTime.plusYears(2L);
        LocalDateTime plusMonthsResult = localDateTime.plusMonths(3L);
        LocalDateTime plusDaysResult = localDateTime.plusDays(7L);
        LocalDateTime plusHoursResult = localDateTime.plusHours(2L);
        LocalDateTime plusMinutesResult = localDateTime.plusMinutes(10L);
        LocalDateTime plusSecondsResult = localDateTime.plusSeconds(10L);
        //减
        LocalDateTime minusYearsResult = localDateTime.minusYears(2L);
        LocalDateTime minusMonthsResult = localDateTime.minusMonths(3L);
        LocalDateTime minusDaysResult = localDateTime.minusDays(7L);
        LocalDateTime minusHoursResult = localDateTime.minusHours(2L);
        LocalDateTime minusMinutesResult = localDateTime.minusMinutes(10L);
        LocalDateTime minusSecondsResult = localDateTime.minusSeconds(10L);

        test01();
        test02();
    }
    public static void test01(){
        LocalDate localDate = LocalDate.now();
//当前时间基础上，指定本年当中的第几天，取值范围为1-365,366
        LocalDate withDayOfYearResult = localDate.withDayOfYear(200);
//当前时间基础上，指定本月当中的第几天，取值范围为1-29,30,31
        LocalDate withDayOfMonthResult = localDate.withDayOfMonth(5);
//当前时间基础上，直接指定年份
        LocalDate withYearResult = localDate.withYear(2017);
//当前时间基础上，直接指定月份
        LocalDate withMonthResult = localDate.withMonth(5);

        System.out.println(localDate);
        System.out.println(withDayOfYearResult);
        System.out.println(withDayOfMonthResult);
        System.out.println(withYearResult);
        System.out.println(withMonthResult);


        LocalTime localTime = LocalTime.now();
//当前时间基础上，直接指定小时
        LocalTime withHourResult = localTime.withHour(1);
//当前时间基础上，直接指定分钟
        LocalTime withMinuteResult = localTime.withMinute(15);
//当前时间基础上，直接指定秒
        LocalTime withSecondResult = localTime.withSecond(20);

        System.out.println(localTime);
        System.out.println(withHourResult);
        System.out.println(withMinuteResult);
        System.out.println(withSecondResult);
    }

    public static void test02(){
        // 本月第一天0:00时刻:
        LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();

// 本月最后1天:
        LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());

// 下月第1天:
        LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());

// 本月第1个周一:
        LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
    }
}
